package com.zhuiyun.project.api.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyun.project.api.caseRecord.entity.CaseRecord;
import com.zhuiyun.project.api.equipment.entity.Equipment;
import com.zhuiyun.project.api.equipment.mapper.EquipmentMapper;
import com.zhuiyun.project.api.equipment.model.EquipmentModel;
import com.zhuiyun.project.api.equipment.service.EquipmentService;
import com.zhuiyun.project.api.venuemanagement.entity.VenueManagement;
import com.zhuiyun.project.api.venuemanagement.mapper.VenueManagementMapper;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.DateUtils;
import com.zhuiyun.project.utils.RedisUtil;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName equipmentServiceImpl
 * @Description TODO 器材管理
 * @Author lpr
 * @Date 2023/11/8 11:05
 **/
@Service
public class  EquipmentServiceImpl implements EquipmentService {
    @Resource
    EquipmentMapper equipmentMapper;
    /**
    * 分页查询+模糊查询 器材管理
    * @Author lpr
    * @Description
    * @Date 2023/11/8 11:29
    * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.zhuiyun.project.api.equipment.entity.Equipment>
    * @param pageRequest
    * @param equipment
    */
    @Override
    public IPage<Equipment> selectEquipment(PageRequest pageRequest, Equipment equipment) {
        List<Equipment> equipments = null;
        equipments = equipmentMapper.selectEquipment(equipment);
        // 设置分页规则
        Page<Equipment> dictDataEntityPage = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        // 当前页码
        int pageNum = pageRequest.getPageNum();
        // 每页记录数
        int pageSize = pageRequest.getPageSize();
        // 计算起始索引
        int startIndex = (pageNum - 1) * pageSize;
        // 计算结束索引
        int endIndex = Math.min(startIndex + pageSize, equipments.size());
        // 截取当前页数据
        List<Equipment> currentPageData = equipments.subList(startIndex, endIndex);
        // 将当前页数据存储到分页对象的记录列表中
        dictDataEntityPage.setRecords(currentPageData);
        // 设置总条数，仍然使用原始的总记录数
        dictDataEntityPage.setTotal(equipments.size());
        return dictDataEntityPage;
    }
    /**
    * 添加器材信息
    * @Author lpr
    * @Description
    * @Date 2023/11/8 15:32
    * @Return
    * @param equipment
    */
    @Override
    @Transactional
    public int addEquipment(MultipartFile[] multipartFiles, Equipment equipment) {
        StringBuilder stringBuilder = new StringBuilder();
        if (multipartFiles != null) {
            for (MultipartFile multipartFile : multipartFiles) {
                String i = UploadFileUtils.uploadFile(multipartFile);
                if (stringBuilder.length() > 0) {
                    stringBuilder.append("," + i);
                } else {
                    stringBuilder.append(i);
                }
            }
        }
        equipment.setDelFlag(0);
        if (stringBuilder.length() > 0) {
            equipment.setEmentPhoto(stringBuilder.toString());
        } else {
            equipment.setEmentPhoto("");
        }
        int insert = equipmentMapper.addEquipment(equipment);
        return insert;
    }

    @Override
    public CommonResult editEquipment(MultipartFile[] files, Equipment equipment) {
        StringBuilder stringBuilder = new StringBuilder();
        if (equipment.getEmentPhoto() != null && !"null".equals(equipment.getEmentPhoto()) && !equipment.getEmentPhoto().isEmpty()) {
            stringBuilder.append(equipment.getEmentPhoto());
        }
        if (equipment.getDelImageIds() != null && !equipment.getDelImageIds().isEmpty() && !"null".equals(equipment.getDelImageIds())) {
            // todo 删除的图片数组
            String[] delImageIds = equipment.getDelImageIds().split(",");
            for (String delImageId : delImageIds) {
                // todo 删除图片
                int i = equipmentMapper.removePhoto(delImageId);
                if (i <= 0) {
                    return CommonResult.error(EmErrorCode.FEIGN_ERROR);
                }
            }
        }
        if (files != null) {
            // todo 上传图片
            for (MultipartFile multipartFile : files) {
                String i = UploadFileUtils.uploadFile(multipartFile);
                if (stringBuilder.length() > 0) {
                    stringBuilder.append("," + i);
                } else {
                    stringBuilder.append(i);
                }
            }
        }
        if (stringBuilder.length() > 0) {
            equipment.setEmentPhoto(stringBuilder.toString());
        } else {
            equipment.setEmentPhoto("");
        }
        int i = equipmentMapper.editEquipment(equipment);
        if (i > 0) {
            // 更新redis当中的缓存数据
            RedisUtil.hdel("Equipment:", String.valueOf(equipment.getEmentId()));
            RedisUtil.hset("Equipment:", String.valueOf(equipment.getEmentId()), equipment, 3600);
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        } else {
            return CommonResult.error(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
     * 修改器材状态
     * @Author lpr
     * @Description
     * @Date 2023/11/08 19:52
     * @Return java.lang.Object
     * @param equipment
     */
    @Override
    public Object editEmentDisplay(Equipment equipment) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("ement_id", equipment.getEmentId());
        return equipmentMapper.update(equipment,wrapper);
    }
    /**
    * 器材逻辑删除
    * @Author lpr
    * @Description
    * @Date 2023/11/9 15:54
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param ementId
    */
    @Override
    public CommonResult removeEmentById(Integer ementId) {
        Equipment equipment = new Equipment();
        equipment.setDelFlag(1);
        UpdateWrapper<Equipment> updateWrapper = Wrappers.update();
        updateWrapper.eq("ement_id",ementId);
        equipmentMapper.update(equipment,updateWrapper);
        return CommonResult.ok(EmErrorCode.DELETE_OK);
    }
    /**
     * 查询 器材管理
     * @Author lpr
     * @Description
     * @Date 2023/11/8 11:29
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.zhuiyun.project.api.equipment.entity.Equipment>
     * @param venueUniqueIdentifier
     */
    @Override
    public List<VenueManagement> listvenueManagementByVenueId(String venueUniqueIdentifier) {
        List<VenueManagement> list = equipmentMapper.listvenueManagementByVenueId(venueUniqueIdentifier);
        return list;
    }
}
